//
//  KLRecommendViewController.m
//  BIBILILI
//
//  Created by juwangkeji on 2018/7/11.
//  Copyright © 2018年 WTF. All rights reserved.
//

#import "KLRecommendViewController.h"
#import "KLRecommendHeaderView.h"
#import "KLRecommendCell.h"
#import <AFNetworking.h>
#import <MJRefreshGifHeader.h>
@interface KLRecommendViewController ()
@property(nonatomic,strong) NSMutableArray * cellArray;
@end

@implementation KLRecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setupUI];
    //开始刷新
//    [self.collectionView.mj_header beginRefreshing];
    
    
}

-(NSMutableArray *)cellArray{
    if (!_cellArray) {
        _cellArray = [NSMutableArray array];
    }
    return _cellArray;
}

-(void)setupRefreshHeader{
    MJRefreshGifHeader * header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    
    UIImage * image1 = [UIImage imageNamed:@"loading1"];
    UIImage * image2 = [UIImage imageNamed:@"loading2"];
    NSArray * imgArray = [NSArray arrayWithObjects:image1,image2, nil];
    [header setImages:imgArray forState: MJRefreshStatePulling];
    self.collectionView.mj_header =header;
}

-(void)loadData{
    
    
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    NSMutableDictionary *param =[NSMutableDictionary dictionary];
    param[@"a"]=@"newlist";
    param[@"c"]=@"data";
    param[@"type"]=@41;//视频
    NSString * url = @"http://api.budejie.com/api/api_open.php";
    [manager GET: url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * responseObject) {
        ;
        /*
        NSLog(@"当前请求：%@", [task.originalRequest.URL.absoluteString stringByAppendingString:task.originalRequest.URL.query.length?[NSString stringWithFormat:@"?%@",task.originalRequest.URL.query]:@""]);
         http://api.budejie.com/api/api_open.php?a=newlist&c=data&type=41?a=newlist&c=data&type=41
         */

        
        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

-(void)setupUI{
    
    //注册重用headerview 和 注册重用cell
    [self.collectionView registerClass:[KLRecommendCell class] forCellWithReuseIdentifier:@"cellID"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    self.collectionView.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
    UICollectionViewFlowLayout * m_layout = (UICollectionViewFlowLayout *) self.collectionView.collectionViewLayout;
    m_layout.itemSize = CGSizeMake((self.view.width-m_layout.minimumLineSpacing-20)/2, __KNewSize(350));
    m_layout.headerReferenceSize = CGSizeMake(self.view.width, __KNewSize(300));
    
    [self setupRefreshHeader];
}



-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 10;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    KLRecommendCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    if (!cell) {
        cell = [[KLRecommendCell alloc] init];
    }
    
    
    return cell;
}

//headerview轮播
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    //headerView轮播
    UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
    
    headerView.frame = CGRectMake(-10, 0, self.view.width, __KNewSize(300));
    NSMutableArray * array = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];
    KLRecommendHeaderView * m_headerView =  [[KLRecommendHeaderView alloc] createAHeaderviewWithImageArray:array frame:CGRectMake(0, 0, self.view.width, __KNewSize(300))];
    [headerView addSubview:m_headerView];
    return headerView;
}

@end
